package edu.snake.implement.view;


import edu.snake.core.model.arena.Arena;
import edu.snake.core.service.ViewController;
import edu.snake.core.view.View;
import edu.snake.implement.ConfigConstant;
import edu.snake.implement.view.inputInterface.MouseController;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import java.awt.Color;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class ViewImpl extends JFrame implements View, ActionListener {

    private JButton startButton;
    private JButton pauseButton;
    private JButton endButton;
    private JLabel scoreCountTextLabel;
    private final ViewController viewController;
    private ViewArena viewArena;

    public ViewImpl(ViewController viewController) throws HeadlessException {

        this.viewController = viewController;
        viewController.addActionListener(this);
        setResizable(false);
        setTitle("Snake");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setBackground(Color.black);
        setFocusable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        initComponent();
    }

    private void initComponent() {
        JPanel controlPanel = new JPanel();


        startButton = new JButton();
        pauseButton = new JButton();
        endButton = new JButton();


        startButton.setText("Start");
        pauseButton.setText("Pause");
        endButton.setText("End");

        pauseButton.setVisible(false);
        endButton.setVisible(false);


        startButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                startButton.setVisible(false);
                pauseButton.setVisible(true);
                endButton.setVisible(true);
                scoreCountTextLabel.setText("0");
                viewController.startGame();//todo createEnum and call one method from controller
            }

        });

        pauseButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                viewController.pauseGame();//todo createEnum and call one method from controller
                String s = pauseButton.getText();
                if (s.equals("Pause")) {
                    pauseButton.setText("Resume");
                    viewArena.drawTextInArena("Pause Game");
                } else {
                    pauseButton.setText("Pause");
                }
            }
        });

        endButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                pauseButton.setVisible(false);
                startButton.setVisible(true);
                endButton.setVisible(false);
                viewArena.drawTextInArena("Game Over");
                viewController.endGameFromViewCommand();//todo createEnum and call one method from controller
            }
        });

        JLabel scoreTextLabel = new JLabel();
        scoreCountTextLabel = new JLabel();
        scoreTextLabel.setText("Score:");
        scoreCountTextLabel.setText("0");


        viewArena = new ViewArena(
                ConfigConstant.cellWidth * ConfigConstant.cellSizePixel
                , ConfigConstant.cellHeight * ConfigConstant.cellSizePixel
        );
        viewArena.setBackground(new Color(51, 51, 51));
        viewArena.setFocusable(true);
        viewArena.addMouseListener(new MouseController(viewController));


        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(controlPanel);
        controlPanel.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(pauseButton
                                                , javax.swing.GroupLayout.PREFERRED_SIZE
                                                , 73
                                                , javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(startButton)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(endButton)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(scoreTextLabel)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(scoreCountTextLabel)
                        )
        );
        jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(startButton)
                                .addComponent(endButton)
                                .addComponent(scoreTextLabel)
                                .addComponent(scoreCountTextLabel)
                                .addComponent(pauseButton))
        );


        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(controlPanel
                                , javax.swing.GroupLayout.DEFAULT_SIZE
                                , javax.swing.GroupLayout.DEFAULT_SIZE
                                , Short.MAX_VALUE)
                        .addComponent(viewArena
                                , javax.swing.GroupLayout.DEFAULT_SIZE
                                , javax.swing.GroupLayout.DEFAULT_SIZE
                                , Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                        .addComponent(controlPanel
                                                , javax.swing.GroupLayout.PREFERRED_SIZE
                                                , javax.swing.GroupLayout.DEFAULT_SIZE
                                                , javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(viewArena
                                                , javax.swing.GroupLayout.DEFAULT_SIZE
                                                , javax.swing.GroupLayout.DEFAULT_SIZE
                                                , Short.MAX_VALUE)
                        )
        );

        pack();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("render")) {
            viewArena.drawArena((Arena) e.getSource());
        } else if (e.getActionCommand().equals("incrementScore")) {
            scoreCountTextLabel.setText((e.getSource()).toString());
        } else if (e.getActionCommand().equals("endGame")) {
            pauseButton.setVisible(false);
            endButton.setVisible(false);
            startButton.setVisible(true);
            viewArena.drawTextInArena("Game Over");
        }
    }
}
